var express = require('express');
var router = express.Router();
var sqlQuery=require('../../module/lcMysql')
var jiami=require('../../module/jiami')
router.get('/',async (req,res)=>{
    let sqlStr="select count(id) as num from user where del!='1' or del is null"
    let result=await sqlQuery(sqlStr)
    let option={
        userList:result[0]
    }
    res.render('user/userList.ejs',option)
})

//用户列表
router.get("/getList",async (req,res)=>{
    let page=parseInt(req.query.page) 
    let limit=req.query.limit
    let username=req.query.username
    var sqlStr,result,sqlStr2,result2;
    if(username){//过滤查询
        //console.log(username);
        sqlStr="select * from user  where username like ? and (del !='1' or del is null) limit ?,?"
        result=await sqlQuery(sqlStr,['%'+username+'%',(page-1)*parseInt(limit) ,parseInt(limit) ])
        sqlStr2="select count(id) as num from user where username like ? and (del !='1' or del is null)"
        result2=await sqlQuery(sqlStr2,['%'+username+'%'])
    }else{
        sqlStr="select * from user  where del !='1' or del is null limit ?,?"
        result=await sqlQuery(sqlStr,[(page-1)*parseInt(limit) ,parseInt(limit) ])
        sqlStr2="select count(id) as num from user where del !='1' or del is null"
        result2=await sqlQuery(sqlStr2)
    }
    let options={
        "code":0,
        "msg":"ok",
        "count":result2[0].num,
        "data":Array.from(result)
    }
    res.json(options)
})

//添加用户
router.get('/add',(req,res)=>{
    res.render('user/addUser.ejs')
})
router.post('/add',async (req,res,next)=>{
    var {username,email,imgheader,mobile,sex,marry,userdesc,roleid,password}=req.body
    let sqlStr='select id from user where username=?'
    let result1=await sqlQuery(sqlStr,[username])
    if(result1.length==0){
        password=jiami(password)
        let sqlStr2="insert into user (username,email,imgheader,mobile,sex,marry,userdesc,roleid,password) values (?,?,?,?,?,?,?,?,?)"
        try {
            await sqlQuery(sqlStr2,[username,email,imgheader,mobile,sex,marry,userdesc,roleid,password])
        } catch (error) {
           // console.log(11);
            
            return next(error)
        }
       res.json({
           msg:'ok',
           content:'数据添加成功'
       })
    }else{
        res.json({
            msg:'fail',
            content:'该用户名已被使用'
            }
        )
    }
    
})
//查看用户
router.get('/detail',async (req,res,next)=>{
    const id=req.query.id
    let sqlStr="select * from user where id=?"
    let sqlStr2="select * from role"
    try {
        var user=await sqlQuery(sqlStr,[id])
        var roles=await sqlQuery(sqlStr2)
    } catch (error) {
        next(error)
    }
    res.render('user/userDetail',{
        user:user[0],
        roles:Array.from(roles)
    })
})
//编辑用户
router.get('/edit',async (req,res,next)=>{
    const id=req.query.id
    let sqlStr="select * from user where id=?"
    let sqlStr2="select * from role"
    try {
        var user=await sqlQuery(sqlStr,[id])
        var roles=await sqlQuery(sqlStr2)
    } catch (error) {
        next(error)
    }
    res.render('user/userEdit',{
        user:user[0],
        roles:Array.from(roles)
    })
})
//修改用户
router.post('/edit',async (req,res,next)=>{
    var {id,imgheader,username,email,mobile,roleid,sex,marry,userdesc}=req.body
    let sqlStr='update user set imgheader=?,username=?,email=?,mobile=?,roleid=?,sex=?,marry=?,userdesc=? where id=?'
    try {
        await sqlQuery(sqlStr,[imgheader,username,email,mobile,roleid,sex,marry,userdesc,id])
    } catch (error) {
        return next(error)
    }
    res.json({
      msg:"ok",
      content:'更新成功'
    })

})
//删除用户(批量,单个删除)
router.get('/del',async (req,res)=>{//备注：del:0  正常  del:1删除
    let ids=req.query.ids
    let sqlStr="update user set del='1' where id in ("+ids+")"
    await sqlQuery(sqlStr)
    res.json({"msg":"ok"})

})
module.exports=router